package com.wn55.hrms.web.controller;

import com.github.pagehelper.PageInfo;
import com.wn55.hrms.entity.Leave;
import com.wn55.hrms.entity.OverTime;
import com.wn55.hrms.service.LeaveService;
import com.wn55.hrms.util.ResultResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Mr.bai~
 * @version 1.0
 * @date 2022/5/6 20:56
 */
@RestController
@RequestMapping("leave")
@Api(value = "本接口是用来操作请假表功能")
public class LeaveController {

    @Autowired
    LeaveService leaveService;

    @GetMapping("/leaveList")
    @ApiOperation(value = "分页查询渠道列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",dataType = "int",name="now",value = "查询当前页",required = false,defaultValue = "1"),
            @ApiImplicitParam(paramType = "query",dataType = "int",name="size",value ="查询每页条数",required = false,defaultValue = "2"),
            @ApiImplicitParam(paramType = "query",dataType = "string",name="eName",value = "员工名称",required = false),
            @ApiImplicitParam(paramType = "query",dataType = "string",name="type",value = "加班类型",required = false),
    })
    public ResultResponse<PageInfo<Leave>> leaveList(
            @RequestParam(value = "now", defaultValue = "1") Integer now,
            @RequestParam(value = "size", defaultValue = "2") Integer size,
            String eName, String type){
        ResultResponse<PageInfo<Leave>> result = null;
        try {

            PageInfo<Leave> pageInfo = leaveService.queryLeave(now, size, eName, type);
            result = new ResultResponse<>(200, "查询成功", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(404, "查询失败");
        }
        return result;
    }
    @PostMapping("/add")
    @ApiOperation(value = "添加请假信息")
    public ResultResponse<Void> addLeave(@RequestBody Leave leave) {
        ResultResponse<Void> result = null;
        try {
            leaveService.addLeave(leave);
            result = new ResultResponse<>(200, "添加请假成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "添加请假失败!");
        }
        return result;
    }

    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据标识ID删除请假记录")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path",dataType = "int",name = "id",value = "请假记录的ID标识")
    })
    public ResultResponse<Void> delete(@PathVariable("id") int id) {

        ResultResponse<Void> result = null;

        try {
            leaveService.removeBykey(id);
            result = new ResultResponse<>(200, "删除请假成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "删除请假失败!");
        }
        return result;
    }

    @PutMapping("/modify")
    @ApiOperation(value = "修改请假记录")
    public ResultResponse<Void> modifyLeave(@RequestBody Leave leave) {
        ResultResponse<Void> result = null;
        try {
            leaveService.modifyLeave(leave);
            result = new ResultResponse<>(200, "修改请假记录成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "修改请假记录失败!");
        }
        return result;
    }
}
